package com.example.schooljava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.schooljava.pojo.entity.Curriculum;
import com.example.schooljava.mapper.CurriculumMapper;
import com.example.schooljava.pojo.bo.CurriculumBO;
import com.example.schooljava.service.CurriculumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.schooljava.util.HttpStatus;
import com.example.schooljava.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LDH
 * @since 2022/08/11
 */
@Service
public class CurriculumServiceImpl extends ServiceImpl<CurriculumMapper, Curriculum> implements CurriculumService {

    @Autowired
    private CurriculumMapper curriculumMapper;

    /**
     * 功能描述: 添加课程<br>
     * @Param: [curriculum]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/22 9:24
     */
    @Override
    public Result setCurriculum(CurriculumBO curriculumBO) {
        QueryWrapper<Curriculum> wrapper = new QueryWrapper<>();
        // 寻找同年级同名字的课程
        wrapper.eq("grade",curriculumBO.getGrade()).eq("name",curriculumBO.getName());
        // 如果找到相同条件的课程，则不保存
        if (list(wrapper).size() != 0) return new Result<>().error("","该课程以存在", HttpStatus.UNAUTHORIZED);
        Curriculum curriculum = new Curriculum();
        curriculum.setGrade(curriculumBO.getGrade());
        curriculum.setName(curriculumBO.getName());
        save(curriculum);
        return new Result<>().OK_Back(true,"班级设立成功！",curriculumMapper.getCurriculum(new CurriculumBO()));
    }

    /**
     * 功能描述: 获取所有课程<br>
     * @Param: []
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/22 9:24
     */
    @Override
    public Result getCurriculum(CurriculumBO curriculumBO) {
        return new Result<>().Huoqu_OK(curriculumMapper.getCurriculum(curriculumBO));
    }

    /**
     * 功能描述: 删除课程<br>
     * @Param: [list]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/22 10:14
     */
    @Override
    public Result delCurriculum(ArrayList<String> list) {
        for (String l : list) {
            for (Curriculum curriculum : list()) {
                if (Long.parseLong(l) == curriculum.getId())
                    removeById(Long.parseLong(l));
            }
        }
        return new Result<>().OK_Back(true,"课程删除成功",curriculumMapper.getCurriculum(new CurriculumBO()));
    }

    /**
     * 功能描述: 修改课程<br>
     * @Param: [curriculumDTO]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/31 20:44
     */
    @Override
    public Result editCurriculum(CurriculumBO curriculumBO) {
        Curriculum curriculum = new Curriculum();
        curriculum.setId(curriculumBO.getId());
        curriculum.setGrade(curriculumBO.getGrade());
        curriculum.setName(curriculumBO.getName());
        updateById(curriculum);
        return new Result<>().OK_Back(true,"课程修改成功",curriculumMapper.getCurriculum(new CurriculumBO()));
    }

    @Override
    public Result getAllCurr() {
        return new Result<>().Huoqu_OK(curriculumMapper.getAllCurr());
    }
}
